package com.tjsinfo.tjsoa.articles.bo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Hibernate;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.articles.dao.IArticlesDao;
import com.tjsinfo.tjsoa.articles.vo.TIndexArticles;
import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingIncept;

public class IArticlesManagerBO implements IArticlesBO{

	private IArticlesDao articlesDao;
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IArticlesDao getArticlesDao() {
		return articlesDao;
	}

	public void setArticlesDao(IArticlesDao articlesDao) {
		this.articlesDao = articlesDao;
	}

	public List findAll(){ 
		return articlesDao.findAll();
	}


	public List<TIndexArticles> findInexByCid(Integer cid,Integer num) {
		// TODO Auto-generated method stub
		List<TIndexArticles> indexArticlesList;
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.eq("cid", cid));
		if(num != 0){
			List<Order> orderlist = new ArrayList<Order>();
			orderlist.add(Order.desc("showOrder"));
			orderlist.add(Order.desc("id"));
			PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
			indexArticlesList = ps.getItems();
		}else{
		criteria.addOrder(Order.desc("showOrder"));
		criteria.addOrder(Order.desc("id"));
		indexArticlesList = qbcDao.getCriteria(criteria);
	    }
		return indexArticlesList;
	}
	public List findInexByCids(Integer scid,Integer ecid,Integer num){
		// TODO Auto-generated method stub
		List<TIndexArticles> indexArticlesList;
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.ge("cid", scid));
		criteria.add(Restrictions.le("cid", ecid));
		if(num != 0){
			List<Order> orderlist = new ArrayList<Order>();
			orderlist.add(Order.desc("showOrder"));
			orderlist.add(Order.desc("id"));
			PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
			indexArticlesList = ps.getItems();
		}else{
		criteria.addOrder(Order.desc("showOrder"));
		criteria.addOrder(Order.desc("id"));
		indexArticlesList = qbcDao.getCriteria(criteria);
	    }
		return indexArticlesList;
	}

	public List findInexByCids(Integer scid1,Integer ecid1,Integer scid2,Integer ecid2,Integer num){
		// TODO Auto-generated method stub
		List<TIndexArticles> indexArticlesList;
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.or(
				Restrictions.between("cid", scid1, ecid1), 
				Restrictions.between("cid", scid2, ecid2)));
		if(num != 0){
			List<Order> orderlist = new ArrayList<Order>();
			orderlist.add(Order.desc("showOrder"));
			orderlist.add(Order.desc("id"));
			PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
			indexArticlesList = ps.getItems();
		}else{
		criteria.addOrder(Order.desc("showOrder"));
		criteria.addOrder(Order.desc("id"));
		indexArticlesList = qbcDao.getCriteria(criteria);
	    }
		return indexArticlesList;
	}
	
	public TIndexArticles findById(String id) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.eq("id", id));
		criteria.addOrder(Order.desc("showOrder"));
		criteria.addOrder(Order.desc("id"));
		List<TIndexArticles> list =  (List<TIndexArticles>)qbcDao.getCriteria(criteria); 
		return list.get(0);
	}

	public Boolean updateArticle(TIndexArticles articles) {
		// TODO Auto-generated method stub
		try {
			hibernateDao.update(articles);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List findByProperty(String propertyName, String value){ 
		return articlesDao.findByProperty(propertyName,value);
	}
	//继承原调用
	public List getIndexFlash(String topN){
		return articlesDao.topNandNot(topN, "1", "1");
	}
	//继承原调用
	public List findList(String value, String value1){ 
		return articlesDao.findList(value,value1);
	}
 	
/** wjj add
 *  根据cid获取该栏目内有效时间在当前日期之后的文章列表
 *  
 */	
	public List<TIndexArticles> findInexByCidDate(Integer cid,Integer num) {
		// TODO Auto-generated method stub
		List indexArticlesList=new ArrayList<TIndexArticles>();
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);

		Date dateToday = new Date();  		//获取当前日期
	
		criteria.add(Restrictions.eq("cid", cid));
		criteria.add(Restrictions.sqlRestriction("CAST({alias}.availableDate AS DateTime)>=?",dateToday,Hibernate.DATE));
		if(num != 0){  
				List<Order> orderlist = new ArrayList<Order>();
				orderlist.add(Order.desc("showOrder"));
				orderlist.add(Order.desc("id"));
				PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
				indexArticlesList = ps.getItems();
			}else{
			criteria.addOrder(Order.desc("showOrder"));
			criteria.addOrder(Order.desc("id"));
			indexArticlesList = qbcDao.getCriteria(criteria);
		    }
		return indexArticlesList;
	}


	/** wjj add
	 *  根据cid获取该栏目内前面num个包含缩略图的文章列表
	 *  
	 */
	public List<TIndexArticles> findNewsPictures(Integer cid,Integer num) {
		// TODO Auto-generated method stub
//		List<TIndexArticles> indexArticlesList;
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.eq("cid", cid));	
		criteria.add(Restrictions.isNotNull("smallPic"));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("showOrder"));
		orderlist.add(Order.desc("id"));	
		qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
		return qbcDao.getCriteria(criteria);	
	}
	
	public List<TIndexArticles> findNewsPictures(Integer scid,Integer ecid,Integer num) {
		// TODO Auto-generated method stub
//		List<TIndexArticles> indexArticlesList;
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.ge("cid", scid));
		criteria.add(Restrictions.le("cid", ecid));
		criteria.add(Restrictions.isNotNull("smallPic"));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("showOrder"));
		orderlist.add(Order.desc("id"));	
		qbcDao.findPageByCriteria(criteria,orderlist, num, 0);
		return qbcDao.getCriteria(criteria);	
	
	}

	public List<TIndexArticles> findNewsPictures(Integer cid) {
		// TODO Auto-generated method stub
		Calendar c = Calendar.getInstance();
 		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		Date date = c.getTime();
 		Date newdate = DayFormat.afterNDay(date, 1);
 		Date Date = DayFormat.afterNDay(date, -90);
 		String dutydate = sdf.format(newdate);
 		String dutydt = sdf.format(Date);
 		
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class);
		criteria.add(Restrictions.eq("cid", cid));	
		criteria.add(Restrictions.isNotNull("smallPic"));
		criteria.add(Restrictions.between("createDate", dutydt, dutydate));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("showOrder"));
		orderlist.add(Order.desc("id"));	
		qbcDao.findPageByCriteria(criteria,orderlist, 60, 0);
		return qbcDao.getCriteria(criteria);	
	}
	
	public List<MeetingIncept> findMeetingList(Integer userId) {
		// TODO Auto-generated method stub
		Date dateToday = new Date();  		//获取当前日期
		String nowSQL = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("nowSQL");
		//翻页
		DetachedCriteria criteria = DetachedCriteria.forClass(MeetingIncept.class);
		DetachedCriteria criteria2 = criteria.createCriteria("meetingRoomEmploy");
		criteria.add(Expression.eq("receiveUserId", userId));
		criteria.add(Expression.eq("sendSql", nowSQL));
		criteria2.add(Expression.ge("startTime", dateToday));
		criteria.addOrder(Order.desc("startTime"));
		List<MeetingIncept> list = qbcDao.getCriteria(criteria);
		return list;
	}
}
